guessing game python

37

import random

num = random.randint(1, 9)

while True:

    try:
        guess = int(input("\nPlease enter a guess from 1-9: "))

        if 0 < guess < 10:

            if guess > num:
                print("\nYou guessed too high")

            elif guess < num:
                print("\nYou guessed too low")

            elif guess == num:
                print("\nYou guessed correctly")

                while True:
                    u_input = input("\nWould you like to play again? y/n: ")

                    if u_input == 'n':
                        exit()

                    elif u_input == 'y':
                        num = random.randint(1, 9)
                        break

                    elif u_input != 'y' and u_input != 'n':
                        print("\nError: Please select a valid option")

        elif guess < 1 or guess > 9:
            print("\nError: Please enter a number from 1-9")

    except ValueError:
        print("\nError: Please enter a number")

Comments

Submit
0 Comments